package com.dynamixsoftware.printershare.smb.dcerpc;

import com.dynamixsoftware.printershare.smb.BufferCache;
import com.dynamixsoftware.printershare.smb.NtlmPasswordAuthentication;
import com.dynamixsoftware.printershare.smb.SmbNamedPipe;
import com.dynamixsoftware.printershare.smb.dcerpc.ndr.NdrBuffer;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.security.Principal;

public abstract class DcerpcHandle
  implements DcerpcConstants
{
  private static int call_id = 1;
  protected DcerpcBinding binding;
  protected int max_recv = this.max_xmit;
  protected int max_xmit = 4280;
  private DcerpcSecurityProvider securityProvider = null;
  protected int state = 0;
  
  private void bind()
    throws DcerpcException, IOException
  {
    try
    {
      this.state = 1;
      sendrecv(new DcerpcBind(this.binding, this));
      return;
    }
    catch (IOException localIOException)
    {
      this.state = 0;
      throw localIOException;
    }
    finally {}
  }
  
  public static DcerpcHandle getHandle(String paramString, NtlmPasswordAuthentication paramNtlmPasswordAuthentication)
    throws UnknownHostException, MalformedURLException, DcerpcException
  {
    if (paramString.startsWith("ncacn_np:")) {
      return new DcerpcPipeHandle(paramString, paramNtlmPasswordAuthentication);
    }
    throw new DcerpcException("DCERPC transport not supported: " + paramString);
  }
  
  protected static DcerpcBinding parseBinding(String paramString)
    throws DcerpcException
  {
    char[] arrayOfChar = paramString.toCharArray();
    String str1 = null;
    String str2 = null;
    DcerpcBinding localDcerpcBinding = null;
    int i = 0;
    int j = 0;
    int k = 0;
    int m = arrayOfChar[i];
    switch (k)
    {
    case 3: 
    case 4: 
    default: 
      i = arrayOfChar.length;
    }
    for (;;)
    {
      i++;
      if (i < arrayOfChar.length) {
        break;
      }
      if ((localDcerpcBinding != null) && (localDcerpcBinding.endpoint != null)) {
        break label298;
      }
      throw new DcerpcException("Invalid binding URL: " + paramString);
      if (m == 58)
      {
        str1 = paramString.substring(j, i);
        j = i + 1;
        k = 1;
        continue;
        if (m == 92)
        {
          j = i + 1;
        }
        else
        {
          k = 2;
          if (m == 91)
          {
            if (paramString.substring(j, i).trim().length() == 0) {}
            localDcerpcBinding = new DcerpcBinding(str1, paramString.substring(j, i));
            j = i + 1;
            k = 5;
            continue;
            if (m == 61)
            {
              str2 = paramString.substring(j, i).trim();
              j = i + 1;
            }
            else if ((m == 44) || (m == 93))
            {
              String str3 = paramString.substring(j, i).trim();
              if (str2 == null) {
                str2 = "endpoint";
              }
              localDcerpcBinding.setOption(str2, str3);
              str2 = null;
            }
          }
        }
      }
    }
    label298:
    return localDcerpcBinding;
  }
  
  public abstract void close()
    throws IOException;
  
  protected abstract void doReceiveFragment(byte[] paramArrayOfByte, boolean paramBoolean)
    throws IOException;
  
  protected abstract void doSendFragment(byte[] paramArrayOfByte, int paramInt1, int paramInt2, boolean paramBoolean)
    throws IOException;
  
  public Principal getPrincipal()
  {
    if ((this instanceof DcerpcPipeHandle)) {
      return ((DcerpcPipeHandle)this).pipe.getPrincipal();
    }
    return null;
  }
  
  public String getServer()
  {
    if ((this instanceof DcerpcPipeHandle)) {
      return ((DcerpcPipeHandle)this).pipe.getServer();
    }
    return null;
  }
  
  public void sendrecv(DcerpcMessage paramDcerpcMessage)
    throws DcerpcException, IOException
  {
    if (this.state == 0) {
      bind();
    }
    boolean bool = true;
    Object localObject1 = BufferCache.getBuffer();
    NdrBuffer localNdrBuffer1;
    try
    {
      localNdrBuffer1 = new NdrBuffer((byte[])localObject1, 0);
      paramDcerpcMessage.flags = 3;
      int i = call_id;
      call_id = i + 1;
      paramDcerpcMessage.call_id = i;
      paramDcerpcMessage.encode(localNdrBuffer1);
      if (this.securityProvider != null)
      {
        localNdrBuffer1.setIndex(0);
        this.securityProvider.wrap(localNdrBuffer1);
      }
      int j = -24 + localNdrBuffer1.getLength();
      int k = 0;
      if (k < j)
      {
        int i1 = j - k;
        if (i1 + 24 > this.max_xmit)
        {
          paramDcerpcMessage.flags = (0xFFFFFFFD & paramDcerpcMessage.flags);
          i1 = -24 + this.max_xmit;
        }
        for (;;)
        {
          paramDcerpcMessage.length = (i1 + 24);
          if (k > 0) {
            paramDcerpcMessage.flags = (0xFFFFFFFE & paramDcerpcMessage.flags);
          }
          if ((0x3 & paramDcerpcMessage.flags) != 3)
          {
            localNdrBuffer1.start = k;
            localNdrBuffer1.reset();
            paramDcerpcMessage.encode_header(localNdrBuffer1);
            localNdrBuffer1.enc_ndr_long(paramDcerpcMessage.alloc_hint);
            localNdrBuffer1.enc_ndr_short(0);
            localNdrBuffer1.enc_ndr_short(paramDcerpcMessage.getOpnum());
          }
          doSendFragment((byte[])localObject1, k, paramDcerpcMessage.length, bool);
          k += i1;
          break;
          paramDcerpcMessage.flags = (0x2 | paramDcerpcMessage.flags);
          paramDcerpcMessage.alloc_hint = i1;
          bool = false;
        }
      }
      doReceiveFragment((byte[])localObject1, bool);
    }
    finally
    {
      BufferCache.releaseBuffer((byte[])localObject1);
    }
    localNdrBuffer1.reset();
    localNdrBuffer1.setIndex(8);
    localNdrBuffer1.setLength(localNdrBuffer1.dec_ndr_short());
    if (this.securityProvider != null) {
      this.securityProvider.unwrap(localNdrBuffer1);
    }
    localNdrBuffer1.setIndex(0);
    paramDcerpcMessage.decode_header(localNdrBuffer1);
    int m = 24;
    if ((paramDcerpcMessage.ptype == 2) && (!paramDcerpcMessage.isFlagSet(2))) {
      m = paramDcerpcMessage.length;
    }
    for (;;)
    {
      byte[] arrayOfByte1;
      NdrBuffer localNdrBuffer2;
      if (!paramDcerpcMessage.isFlagSet(2))
      {
        if (arrayOfByte1 == null)
        {
          arrayOfByte1 = new byte[this.max_recv];
          localNdrBuffer2 = new NdrBuffer(arrayOfByte1, 0);
        }
        doReceiveFragment(arrayOfByte1, bool);
        localNdrBuffer2.reset();
        localNdrBuffer2.setIndex(8);
        localNdrBuffer2.setLength(localNdrBuffer2.dec_ndr_short());
        if (this.securityProvider != null) {
          this.securityProvider.unwrap(localNdrBuffer2);
        }
        localNdrBuffer2.reset();
        paramDcerpcMessage.decode_header(localNdrBuffer2);
        int n = -24 + paramDcerpcMessage.length;
        if (m + n > localObject1.length)
        {
          byte[] arrayOfByte2 = new byte[m + n];
          System.arraycopy(localObject1, 0, arrayOfByte2, 0, m);
          localObject1 = arrayOfByte2;
        }
        System.arraycopy(arrayOfByte1, 24, localObject1, m, n);
        m += n;
      }
      else
      {
        paramDcerpcMessage.decode(new NdrBuffer((byte[])localObject1, 0));
        BufferCache.releaseBuffer((byte[])localObject1);
        DcerpcException localDcerpcException = paramDcerpcMessage.getResult();
        if (localDcerpcException != null) {
          throw localDcerpcException;
        }
        return;
        localNdrBuffer2 = null;
        arrayOfByte1 = null;
      }
    }
  }
  
  public void setDcerpcSecurityProvider(DcerpcSecurityProvider paramDcerpcSecurityProvider)
  {
    this.securityProvider = paramDcerpcSecurityProvider;
  }
  
  public String toString()
  {
    return this.binding.toString();
  }
}


/* Location:           C:\Users\Admin\Desktop\Decompiler\JAD\jd-gui-0.3.6.windows\classes_dex2jar.jar
 * Qualified Name:     com.dynamixsoftware.printershare.smb.dcerpc.DcerpcHandle
 * JD-Core Version:    0.7.0.1
 */